package com.angel.controller;

import com.angel.bean.SysmptomType;
import com.angel.service.OtherlogService;
import com.angel.service.SysmptomTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询症状并且分页显示
 */
@Controller
@RequestMapping("/SysmpType")
public class SysmpTypeController {
    @Autowired
    private SysmptomTypeService sysmptomTypeService;
    @Autowired
    private OtherlogService otherlogService;
    /**
     * 查询症状并且分页显示
     * @param posion
     * @param model
     * @return
     */
    @RequestMapping("/FindSysmp")
    public String FindSysmp(@RequestParam(value = "posion", defaultValue = "1") String posion, Model model) {
       // System.out.print("--------------------------------查询症状");
        int tempn = 1;
        try {
            tempn = Integer.valueOf(posion);
        } catch (Exception e) {
        }
        PageHelper.startPage(tempn, 10);
        //获取
        List<SysmptomType> sysmptomTypeList = sysmptomTypeService.findAll(null);
        //分页
        PageInfo page = new PageInfo(sysmptomTypeList);
        page.setList(sysmptomTypeList);
        if (page != null) {
            model.addAttribute("pageInfo", page);
        }
        return "template_addsysmp";
    }
    /**
     * 添加症状
     *
     * @return
     */
    @RequestMapping("/addSysmptom")
    public Object addSysmptom(SysmptomType sysmptomType) {
        Map<String, Object> map = new HashMap<>();
        //System.out.print("-------------------------准备添加症状");
        sysmptomType.setSysmpName(sysmptomType.getSysmpName().replace(" ",""));
        sysmptomType.setSysmpInstruct(sysmptomType.getSysmpInstruct().replace(" ",""));
        sysmptomType.setState(1);
        sysmptomType.setUseState(1);
       // System.out.print("准备添加症状---名称-" + sysmptomType.getSysmpName() + "添加的类型" + sysmptomType.getReserve1());
        int result = sysmptomTypeService.insert(sysmptomType);
        if (result != 0) {
            map.put("data", "true");
            System.out.print("添加成功");
        } else {
            map.put("data", "false");
            System.out.print("添加失败");
        }
        return map;
    }

    /**
     * 删除症状类型
     *
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/deletSysmpType")
    @ResponseBody
    public Object deletSysmpType(SysmptomType sysmptomType) {
        //System.out.print("-----执行删除------------");
        sysmptomType.setState(0);
        Map<String, Object> map = new HashMap<>();
        int relsut = sysmptomTypeService.deletOneId(sysmptomType);
        if (relsut != 0) {
            return map.put("data", "true");
        } else {
            return map.put("data", "false");
        }
    }

    /**
     * 查询单症状
     *
     * @return
     */
    @RequestMapping("/findOnl")
    @ResponseBody
    public Object findOnl(Integer systypeid) {
        //System.out.println("----------------------------查询单个穴位" + systypeid);
        Map<String, Object> map = new HashMap<>();
        SysmptomType sysmptomType = new SysmptomType();
        sysmptomType.setSysmpTypeId(systypeid);
        SysmptomType emp = sysmptomTypeService.findOne(sysmptomType);
        if (emp != null) {
            map.put("emp", emp);
        } else {
            map.put("emp", "false");
        }
       // System.out.println("--id" + emp.getSysmpTypeId() + "和" + emp.getSysmpName());
        return map;
    }

    /**
     * 症状更新
     */
    @RequestMapping("/updateSysmptom")
    public Object updateSysmptom(SysmptomType sysmptomType) {
       // System.out.print("---------------症状更新" + sysmptomType.getSysmpTypeId() + "的id");
        sysmptomType.setSysmpName(sysmptomType.getSysmpName().replace(" ",""));
        sysmptomType.setSysmpInstruct(sysmptomType.getSysmpInstruct().replace(" ",""));
        sysmptomType.setState(1);
        Map<String, Object> map = new HashMap<>();
        int result = sysmptomTypeService.update(sysmptomType);
        if (result != 0) {
            return map.put("data", "true");
        } else {
            return map.put("data", "false");
        }
    }

    /**
     * 名称校验
     * @return
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public Object check_name(String sysmpName){
        //System.out.println("------------查询症状名称------是否可以--------"+sysmpName);
        boolean type=true;
        Map<String,Object> map=new HashMap<>();
        List<SysmptomType> sysmptomTypeList= sysmptomTypeService.findAll(null);
        for (int i=0;i<sysmptomTypeList.size();i++){
            if ( sysmptomTypeList.get(i).getSysmpName().equals(sysmpName) ) {
                type=false;
               // System.out.println("------------症状名称不可用--------------"+type);
                break;
            }
        }
        if (type){
            map.put("emp", "true");
            //System.out.println("------------可用--------------"+type);
        }else {
            map.put("emp", "false");
        }
        return map;
    }

    /**
     *启动和禁用
     * @param sysmpTypeId
     * @param state
     * @return
     */
    @RequestMapping("/stateSysmptom")
    public Object state_id(Integer sysmpTypeId,Integer state){
        Map<String,String> emp=new HashMap<>();
        System.out.print(sysmpTypeId+"和------s"+state);
        SysmptomType sysmptomType=new SysmptomType();
        sysmptomType.setSysmpTypeId(sysmpTypeId);
        if (state==1){
            sysmptomType.setState(0);
        }else {
            sysmptomType.setState(1);
        }
        sysmptomTypeService.updatestate(sysmptomType);
        return  emp;
    }
    /**
     * 动态添加症状的模糊查询
     * @param content
     * @param model
     */
    @RequestMapping("/searchSysmptomType")
    public String searchSysmptomType(@RequestParam(value="posion" ,defaultValue = "1") String posion,String content, Model model){
        //System.out.print(content+"--------------------------");
        int pono=1;
        try {
            pono=Integer.valueOf(posion);
        }catch (Exception e){

        }
        SysmptomType sysmptomType=new SysmptomType();
        sysmptomType.setSysmpName("%"+content+"%");
        PageHelper.startPage(pono, 10);
        List<SysmptomType> sysmptomTypeList=sysmptomTypeService.searchSysmptomType(sysmptomType);
        PageInfo pageInfo=new PageInfo(sysmptomTypeList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("content",content);
        return "template_addsysmp2";
    }
}
